from flask import Flask,jsonify,render_template,request,escape
from gaodeapi import weather
import pandas as pd

app = Flask(__name__)

# 调用日志记录函数
def log_request(req:'flask_request',res:str)->None:
	with open('vsearch.txt','a') as log:
		print(req.form,req.remote_addr,req.user_agent,res,file=log,sep='|')

# 进入网页的首页，跳转至entry.html
@app.route('/')
def entry() ->'html':
	return render_template('entry.html',the_title='Welcome to the web!')


# 点击“查询”后，调用高德天气查询api，跳转至entry2.html
@app.route('/ob',methods=['POST'])
def go_search():
	city = request.form['city']
	tianqi = weather(city,key="d51fbd0868d98deca1f02899f79c6e8e")
	province1 = tianqi["lives"][0]['province']
	city1 = tianqi["lives"][0]['city']
	adcode1 = tianqi["lives"][0]['adcode']
	tq1 = tianqi["lives"][0]['weather']
	temperature1 = tianqi["lives"][0]['temperature']
	winddirection1 = tianqi["lives"][0]['winddirection']
	windpower1 = tianqi["lives"][0]['windpower']
	humidity1 = tianqi["lives"][0]['humidity']
	reporttime1 = tianqi["lives"][0]['reporttime']
	return render_template('entry2.html',the_title='为您返回的天气查询情况：',the_city = tianqi,
			province = province1,
			city = city1,
			adcode = adcode1,
			weather = tq1,
			temperature = temperature1,
			winddirection = winddirection1,
			windpower = windpower1,
			humidity = humidity1,
			reporttime = reporttime1)


# 查看日志
@app.route('/viewlog')
def view_the_log() ->'html':
	contents = []
	with open('vsearch.txt') as log:
		for line in log:
			contents.append([])
			for item in line.split('|'):
				contents[-1].append(escape(item))
	titles = ('Form Data','Remote_addr','User_agent','Results')
	return render_template('viewlog.html',
		the_title='View Log',
		the_row_titles=titles,
		the_data=contents,)

app.run(debug=True)